import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { NzMessageService, UploadChangeParam, UploadFile } from 'ng-zorro-antd';
import { FetchService } from '../../../core/net/fetch.service';
import { Router } from '@angular/router';
export interface Data {
    id: number;
    name: string;
}
@Component({
  selector: 'app-pm-version-mng',
  templateUrl: './pm-version-mng.component.html',
  styleUrls: ['./pm-version-mng.component.scss']
})
export class PmVersionMngComponent implements OnInit {
    searchForm1: FormGroup;
    searchForm2: FormGroup;
    tableData: Data[] = [];
    tableData2 = [];
    yearOption = [
        {value: '', label: '全部'},
        {value: 2019, label: '2019'},
        {value: 2020, label: '2020'},
    ];
    typeOption = [
        {value: '', label: '全部'},
        {value: '1', label: '类型一'},
        {value: '2', label: '类型二'},
    ];
    form: FormGroup;
    fileList: UploadFile[] = [];
    uploadUrl = `${(window as any).environment.apiBaseUrl}comm/file/upload`;
    fileUploadForm: FormGroup;
    fileModalTitle = '文件上传';
    fileModalShow = false;
    uploadFileId = '';
    nzBodyStyle: any = {
        padding: '16px 20px 0'
    };
    selectedId = '';
    searchName = '';
    constructor(private fb: FormBuilder,
                private fetchService: FetchService,
                private router: Router,
                private message: NzMessageService) {
        this.searchForm1 = this.fb.group({
            name: [''],
            year: [''],
            type: ['']
        });

        this.fileUploadForm = this.fb.group({
            projectId: [null],
            fileName: [null],
            fileTypeName: [null],
            num: [null],
            version: [null]
        });
    }

    ngOnInit(): void {
        this.queryInfo();
    }

    queryInfo(): void {
        this.fetchService.post<any>('archives/wrsProjectInfo/list', this.searchForm1.value).subscribe(res => {
            this.tableData = res.list;
        });
    }
    queryInfoActive(): void {
        console.log(this.searchName);
        this.fetchService.get<any>(`archives/wrsProjectInfo/${this.selectedId}?fileTypeName=${this.searchName}`).subscribe(res => {
            this.tableData2 = res.projectFiles;
        });
    }

    checkDetail(): void {
        this.fetchService.get<any>(`archives/wrsProjectInfo/${this.selectedId}`).subscribe(res => {
            this.tableData2 = res.projectFiles;
        });
    }

    onDelete(id) {
        this.fetchService.post('archives/wrsProjectInfo/delete', {ids: [id]}).subscribe(resp => {
            this.message.info('删除成功');
            this.queryInfo();
        });
    }

    addProj(): void {
        this.router.navigate(['/pm/file-detail'], {queryParams: {id: ''}});
    }

    onConfirm() {
        if (!this.form.valid) {
            this.message.warning('信息填写有误');
            return;
        }
        const data = this.form.value;
        if (data.id) {
            this.fetchService.post('archives/wrsProjectInfo/edit', data).subscribe(res => {
                this.message.success('操作成功！');
            });
        } else {
            this.fetchService.post('archives/wrsProjectInfo/add', data).subscribe(res => {
                this.message.success('操作成功！');
            });
        }
    }

    onFileConfirm(): void {
        if (!this.uploadFileId) {
            this.message.error('请上传附件');
            return;
        }
        const param = {
            projectId: this.fileUploadForm.value.projectId,
            fileId: this.uploadFileId,
            num: this.fileUploadForm.value.num,
            version: this.fileUploadForm.value.version
        };
        this.fetchService.post('archives/wrsProjectFile/add', param).subscribe(res => {
            this.message.success('操作成功！');
            this.fileModalShow = false;
            this.checkDetail();
        });
    }

    onFileCancel(): void {
        this.fileModalShow = false;
    }

    handleChange({file, fileList}: UploadChangeParam): void {
        const status = file.status;
        this.uploadFileId = '';
        if (status !== 'uploading') {
            // console.log(file, fileList);
        }
        if (status === 'done') {
            this.uploadFileId = file.response.data.id;
        } else if (status === 'error') {

        }
    }

    versionManage(item) {
        this.router.navigate(['/pm/history-version'], {queryParams: {projectId: item.projectId, num: item.num}});
    }

    cellClick(data): void {
        this.selectedId = data.id;
        this.checkDetail();
    }

    openFileUploadModal(item) {
        this.fileUploadForm.patchValue(item);
        this.fileList = [];
        this.fileModalShow = true;
    }

}
